unit uCad_DeclaracaoImportacao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvPanel, DBCtrls, StdCtrls, Mask, dxGDIPlusClasses,
  JvExControls, JvXPCore, JvXPButtons, ExtCtrls, JvExExtCtrls,
  JvExtComponent, JvFooter, Grids, DBGrids, ComCtrls, JvExComCtrls,
  JvComCtrls, CurrEdit, ToolEdit, DB, DBClient, DBTables;

type
  TfrmCad_DeclaracaoImportacao = class(TForm)
    pnlCtrlPaginaDeclaracaoImportacao: TJvPageControl;
    pgVisualizarDeclaracaoImportacao: TTabSheet;
    grDeclImpor: TDBGrid;
    rodapeEdicaoDeclaImport: TJvFooter;
    btIncluirDeclImport: TJvXPButton;
    btEliminarDeclImport: TJvXPButton;
    btEditarDeclImport: TJvXPButton;
    pgEdicaoDeclaracaoImportacao: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    edtNDI: TDBEdit;
    edtCExportador: TDBEdit;
    grbDesemAduaneiro: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    edtLocalDembaraco: TDBEdit;
    grbAdicoes: TGroupBox;
    dbgAdicoes: TDBGrid;
    pnlAdicoes: TJvFooter;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    edtNumAdicao: TDBEdit;
    edtCodFabric: TDBEdit;
    btOkAdicoes: TJvXPButton;
    btCancelaAdicoes: TJvXPButton;
    rodapeedicaoDeclaracaoImportacao: TJvFooter;
    btOkDeclaracaoImportacao: TJvXPButton;
    btCancelarDeclaracaoImportacao: TJvXPButton;
    JvPanel1: TJvPanel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    lblSerie: TLabel;
    lblNumNF: TLabel;
    lblItem: TLabel;
    lblFornecedor: TLabel;
    edtDI: TDateEdit;
    cmbUF: TComboBox;
    edtDataDesemb: TDateEdit;
    edtVlrDesc: TCurrencyEdit;
    CDSDi: TClientDataSet;
    CDSAd: TClientDataSet;
    CDSDinDI: TStringField;
    CDSDidDi: TDateField;
    CDSDixLocDesemb: TStringField;
    CDSDiUFDesemb: TStringField;
    CDSDidDesemb: TDateField;
    CDSDicExportador: TStringField;
    CDSDiNotc_Item: TStringField;
    CDSAdNotc_Item: TStringField;
    CDSAdnDI: TStringField;
    CDSAdnAdicao: TFloatField;
    CDSAdnSeqAdic: TFloatField;
    CDSAdcFabricante: TStringField;
    CDSAdvDescDI: TFloatField;
    dsDI: TDataSource;
    dsAd: TDataSource;
    Bevel1: TBevel;
    rodapeInclusaoAdicao: TJvFooter;
    btIncluirAdicoes: TJvXPButton;
    btEliminarAdicoes: TJvXPButton;
    btEditarAdicoes: TJvXPButton;
    bFechar: TJvXPButton;
    CDSDicodDocImportacao: TStringField;
    CDSDiNum_AcDraw: TStringField;
    Label14: TLabel;
    cmbTipo: TComboBox;
    edtNumDraw: TDBEdit;
    Label15: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure dbgAdicoesCellClick(Column: TColumn);
    procedure grDeclImporCellClick(Column: TColumn);
    procedure btIncluirDeclImportClick(Sender: TObject);
    procedure btEditarDeclImportClick(Sender: TObject);
    procedure btEliminarDeclImportClick(Sender: TObject);
    procedure btOkDeclaracaoImportacaoClick(Sender: TObject);
    procedure btCancelarDeclaracaoImportacaoClick(Sender: TObject);
    procedure btIncluirAdicoesClick(Sender: TObject);
    procedure btEditarAdicoesClick(Sender: TObject);
    procedure btEliminarAdicoesClick(Sender: TObject);
    procedure btOkAdicoesClick(Sender: TObject);
    procedure btCancelaAdicoesClick(Sender: TObject);
    procedure CDSDiBeforeDelete(DataSet: TDataSet);
    procedure CDSAdBeforePost(DataSet: TDataSet);
    procedure pnlCtrlPaginaDeclaracaoImportacaoChanging(Sender: TObject;
      var AllowChange: Boolean);
    procedure bFecharClick(Sender: TObject);
  private
    { Private declarations }
    procedure carregaUF();
  public
    { Public declarations }
    pNOTC_SERIE, pNOTAC_NUMERO, pENT_CODIGO, pNOTC_ITEM, pProduto: string;
  end;

var
  frmCad_DeclaracaoImportacao: TfrmCad_DeclaracaoImportacao;

implementation

uses Dm;

{$R *.dfm}

procedure TfrmCad_DeclaracaoImportacao.FormActivate(Sender: TObject);
begin
  pnlCtrlPaginaDeclaracaoImportacao.ActivePage := pgVisualizarDeclaracaoImportacao;

  CDSDi.Filtered := False;
  CDSDi.Filter := 'Notc_Item = ' + QuotedStr(pNOTC_ITEM);
  CDSDi.Filtered := True;

  CDSAd.Filtered := False;
  CDSAd.Filter := 'Notc_item = ' + QuotedStr(CDSDiNotc_Item.AsString) + ' and nDI = ' + QuotedStr(CDSDinDI.AsString);
  CDSAd.Filtered := True;

  carregaUF();
end;

procedure TfrmCad_DeclaracaoImportacao.dbgAdicoesCellClick(
  Column: TColumn);
begin
  edtVlrDesc.Value := CDSAdvDescDI.AsFloat;
end;

procedure TfrmCad_DeclaracaoImportacao.carregaUF;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Close;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select Est_sigla from estado order by Est_Sigla');
  tq.Open;
  cmbUF.Items.Clear;
  while not tq.Eof do begin
    cmbUF.Items.Add(tq.FieldByName('est_sigla').AsString);
    tq.Next;
  end;
  tq.Close;
  tq.Free;
  cmbUF.ItemIndex := -1;
end;

procedure TfrmCad_DeclaracaoImportacao.grDeclImporCellClick(
  Column: TColumn);
begin
  edtDI.Date := CDSDidDi.AsDateTime;
  edtDataDesemb.Date := CDSDidDesemb.AsDateTime;
  cmbUF.ItemIndex := cmbUF.Items.IndexOf(CDSDiUFDesemb.AsString);
  cmbTipo.ItemIndex := StrToIntDef(CDSDicodDocImportacao.AsString, 0);
end;

procedure TfrmCad_DeclaracaoImportacao.btIncluirDeclImportClick(
  Sender: TObject);
begin
  pnlCtrlPaginaDeclaracaoImportacao.ActivePage := pgEdicaoDeclaracaoImportacao;
  CDSDi.Append;
  edtDI.Clear;
  edtDataDesemb.Clear;

  CDSDiNotc_Item.AsString := pNOTC_ITEM;
  edtNDI.SetFocus;
  pnlAdicoes.Enabled := False;
end;

procedure TfrmCad_DeclaracaoImportacao.btEditarDeclImportClick(
  Sender: TObject);
begin
  if not CDSDi.IsEmpty then begin
    pnlCtrlPaginaDeclaracaoImportacao.ActivePage := pgEdicaoDeclaracaoImportacao;
    edtNDI.SetFocus;
    pnlAdicoes.Enabled := False;
    CDSAd.Filtered := False;
    CDSAd.Filter := 'Notc_item = ' + QuotedStr(CDSDiNotc_Item.AsString) + ' and nDI = ' + QuotedStr(CDSDinDI.AsString);
    CDSAd.Filtered := True;
    CDSDi.Edit;
  end;

end;

procedure TfrmCad_DeclaracaoImportacao.btEliminarDeclImportClick(
  Sender: TObject);
begin
  if not CDSDi.IsEmpty then begin

    CDSDi.Delete;
  end;
end;

procedure TfrmCad_DeclaracaoImportacao.btOkDeclaracaoImportacaoClick(
  Sender: TObject);
begin
  if CDSDi.State in [dsInsert, dsEdit] then begin
    CDSDidDi.AsDateTime := edtDI.Date;
    CDSDidDesemb.AsDateTime := edtDataDesemb.Date;
    CDSDiUFDesemb.AsString := cmbUF.Items.Strings[cmbUF.itemIndex];
    CDSDicodDocImportacao.AsString := IntToStr(cmbTipo.ItemIndex);
    CDSDi.Post;
    pnlCtrlPaginaDeclaracaoImportacao.ActivePage := pgVisualizarDeclaracaoImportacao;
  end;

end;

procedure TfrmCad_DeclaracaoImportacao.btCancelarDeclaracaoImportacaoClick(
  Sender: TObject);
begin
  if CDSDi.State in [dsInsert, dsEdit] then begin
    CDSDi.Cancel;
    pnlCtrlPaginaDeclaracaoImportacao.ActivePage := pgVisualizarDeclaracaoImportacao;
  end;
end;

procedure TfrmCad_DeclaracaoImportacao.btIncluirAdicoesClick(
  Sender: TObject);
var
  maiorNumeroItemAtual: Integer;
begin
  inherited;
  //Identificador do Item
  maiorNumeroItemAtual := 0;
  CDSAd.First;
  while not CDSAd.Eof do begin
    if CDSAdnSeqAdic.AsInteger > maiorNumeroItemAtual then
      maiorNumeroItemAtual := CDSAdnSeqAdic.AsInteger;
    CDSAd.Next;
  end;
  inc(maiorNumeroItemAtual);
  pnlAdicoes.Enabled := True;
  edtNumAdicao.SetFocus;
  CDSAd.Append;
  edtVlrDesc.Clear;
  CDSAdnSeqAdic.AsInteger := maiorNumeroItemAtual;
end;

procedure TfrmCad_DeclaracaoImportacao.btEditarAdicoesClick(
  Sender: TObject);
begin
  if not CDSAd.IsEmpty then begin
    pnlAdicoes.Enabled := True;
    edtNumAdicao.SetFocus;
    CDSAd.Edit;
  end;
end;

procedure TfrmCad_DeclaracaoImportacao.btEliminarAdicoesClick(
  Sender: TObject);
begin
  if not CDSAd.IsEmpty then
    CDSAd.Delete;
end;

procedure TfrmCad_DeclaracaoImportacao.btOkAdicoesClick(Sender: TObject);
begin
  if CDSAd.State in [dsInsert, dsEdit] then begin
    CDSAdvDescDI.AsFloat := edtVlrDesc.Value;
    pnlAdicoes.Enabled := False;
    CDSAd.Post;
  end;
  CDSAd.Filtered := False;
  CDSAd.Filter := 'Notc_item = ' + QuotedStr(CDSDiNotc_Item.AsString) + ' and nDI = ' + QuotedStr(CDSDinDI.AsString);
  CDSAd.Filtered := True;
end;

procedure TfrmCad_DeclaracaoImportacao.btCancelaAdicoesClick(
  Sender: TObject);
begin
  if CDSAd.State in [dsInsert, dsEdit] then begin
    pnlAdicoes.Enabled := False;
    CDSAd.Cancel;
  end;
end;

procedure TfrmCad_DeclaracaoImportacao.CDSDiBeforeDelete(
  DataSet: TDataSet);
begin
  CDSAd.Filtered := False;
  CDSAd.Filter := 'Notc_item = ' + QuotedStr(CDSDiNotc_Item.AsString) + ' and nDI = ' + QuotedStr(CDSDinDI.AsString);
  CDSAd.Filtered := True;
  CDSAd.First;
  while not CDSAd.Eof do begin
    CDSAd.Delete;
  end;
end;

procedure TfrmCad_DeclaracaoImportacao.CDSAdBeforePost(DataSet: TDataSet);
begin
  CDSAdnDI.AsString := CDSDinDI.AsString;
  CDSAdNotc_Item.AsString := CDSDiNotc_Item.AsString;
end;

procedure TfrmCad_DeclaracaoImportacao.pnlCtrlPaginaDeclaracaoImportacaoChanging(
  Sender: TObject; var AllowChange: Boolean);
begin
  AllowChange := CDSDi.State in [dsInsert, dsEdit];
end;

procedure TfrmCad_DeclaracaoImportacao.bFecharClick(Sender: TObject);
begin
  Close;
end;

end.

